package com.itstudy.controller;
import com.itstudy.pojo.Result;
import com.itstudy.pojo.User;
import com.itstudy.service.UserService;
import com.itstudy.utils.JwtUtil;
import com.itstudy.utils.Md5Util;
import com.itstudy.utils.ThreadLocalUtil;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Pattern;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("user")
@Validated
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @PostMapping("register")
    public Result Register(@Pattern(regexp = "^\\S{1,16}$")  @RequestParam("username") String userName, @Pattern(regexp = "^\\S{1,16}$")  @RequestParam("password")  String passWord) {
        User user= userService.selectByName(userName);
        if (user == null) {
            userService.insertIntoUser(userName,passWord);
            return Result.success();
        }
        return Result.error("用户名已存在");
    }

    @PostMapping("login")
    public Result Login(String username,String password) {
        User user=userService.selectByName(username);
        if (user == null) {
            return Result.error("无该用户名");
        }
        if (!Md5Util.getMD5String(password).equals(user.getPassword())) {
            return Result.error("密码输入错误");
        }
        Map claims=new HashMap<>();
        claims.put("id",user.getId());
        claims.put("username",user.getUsername());
        String token= JwtUtil.genToken(claims);
        //将token存储到redis中
        ValueOperations<String,String> operations=stringRedisTemplate.opsForValue();
        operations.set(token,token,1, TimeUnit.HOURS);
        return Result.success(token);
    }

    @GetMapping("userInfo")
    public Result<User> getUserInfo(/*@RequestHeader(name="Authorization") String name*/) {
       /* System.out.println(name);
        Map<String, Object> map = JwtUtil.parseToken(name);*/
//        String username = (String) map.get("username");
        Map<String,Object> map = ThreadLocalUtil.get();
        String username= (String) map.get("username");
        User user = userService.selectByName(username);
        return Result.success(user);
    }

    @PutMapping("update")
    public Result update(@Validated @RequestBody User user) {
        userService.update(user);
        return Result.success();
    }

    @PatchMapping("updateAvatar")
    public Result updateAvatar(@RequestParam @URL String avatarUrl) {
        userService.updateAvatar(avatarUrl);
        return Result.success();
    }

    @PatchMapping("updatePwd")
    public Result updatePwd(@RequestBody Map<String,Object> map,@RequestHeader("Authorization")  String token) {
        String old_pwd = (String) map.get("old_pwd");
        String new_pwd = (String) map.get("new_pwd");
        String re_pwd= (String) map.get("re_pwd");

        if (!StringUtils.hasLength(new_pwd)||!StringUtils.hasLength(old_pwd)||!StringUtils.hasLength(re_pwd)) {
            return Result.error("请输入正确的格式");
        }
        if (!new_pwd.equals(re_pwd)) {
            return Result.error("两次密码不一致");
        }
        Map<String,Object> map1=JwtUtil.parseToken(token);
        String name= (String) map1.get("username");
        Integer id= (Integer) map1.get("id");
        User user = userService.selectByName(name);
        if (user.getPassword().equals(Md5Util.getMD5String(old_pwd))) {
            userService.updatePwd(Md5Util.getMD5String(re_pwd),id);
            return Result.success();
        }
        return Result.error("原始密码输入错误!");
    }
}
